#ifndef COMMON_H
#define COMMON_H

#include <stdarg.h>
#include <stdio.h>

#define MCLK 60000000

#define rWTCON      (*(volatile unsigned *)0x1d30000)
#define rWTDAT      (*(volatile unsigned *)0x1d30004)
#define rWTCNT      (*(volatile unsigned *)0x1d30008)

#define rULCON0     (*(volatile unsigned *)0x1d00000)
#define rULCON1     (*(volatile unsigned *)0x1d04000)
#define rUCON0      (*(volatile unsigned *)0x1d00004)
#define rUCON1      (*(volatile unsigned *)0x1d04004)
#define rUFCON0     (*(volatile unsigned *)0x1d00008)
#define rUFCON1     (*(volatile unsigned *)0x1d04008)
#define rUMCON0     (*(volatile unsigned *)0x1d0000c)
#define rUMCON1     (*(volatile unsigned *)0x1d0400c)
#define rUTRSTAT0   (*(volatile unsigned *)0x1d00010)
#define rUTRSTAT1   (*(volatile unsigned *)0x1d04010)
#define rUERSTAT0   (*(volatile unsigned *)0x1d00014)
#define rUERSTAT1   (*(volatile unsigned *)0x1d04014)
#define rUFSTAT0    (*(volatile unsigned *)0x1d00018)
#define rUFSTAT1    (*(volatile unsigned *)0x1d04018)
#define rUMSTAT0    (*(volatile unsigned *)0x1d0001c)
#define rUMSTAT1    (*(volatile unsigned *)0x1d0401c)
#define rUBRDIV0    (*(volatile unsigned *)0x1d00028)
#define rUBRDIV1    (*(volatile unsigned *)0x1d04028)

#define rUTXH0      (*(volatile unsigned char *)0x1d00020)
#define rUTXH1      (*(volatile unsigned char *)0x1d04020)
#define rURXH0      (*(volatile unsigned char *)0x1d00024)
#define rURXH1      (*(volatile unsigned char *)0x1d04024)
#define WrUTXH0(ch) (*(volatile unsigned char *)0x1d00020)=(unsigned char)(ch)
#define WrUTXH1(ch) (*(volatile unsigned char *)0x1d04020)=(unsigned char)(ch)
#define RdURXH0()   (*(volatile unsigned char *)0x1d00024)
#define RdURXH1()   (*(volatile unsigned char *)0x1d04024)
#define UTXH0       (0x1d00020)    //byte_access address by BDMA
#define UTXH1       (0x1d04020)
#define URXH0       (0x1d00024)
#define URXH1       (0x1d04024)


/* I/O PORT */
#define rPCONA		(*(volatile unsigned *)0x1d20000)
#define rPDATA		(*(volatile unsigned *)0x1d20004)

#define rPCONB		(*(volatile unsigned *)0x1d20008)
#define rPDATB		(*(volatile unsigned *)0x1d2000c)

#define rPCONC		(*(volatile unsigned *)0x1d20010)
#define rPDATC		(*(volatile unsigned *)0x1d20014)
#define rPUPC		(*(volatile unsigned *)0x1d20018)

#define rPCOND		(*(volatile unsigned *)0x1d2001c)
#define rPDATD		(*(volatile unsigned *)0x1d20020)
#define rPUPD		(*(volatile unsigned *)0x1d20024)

#define rPCONE		(*(volatile unsigned *)0x1d20028)
#define rPDATE		(*(volatile unsigned *)0x1d2002c)
#define rPUPE		(*(volatile unsigned *)0x1d20030)

#define rPCONF		(*(volatile unsigned *)0x1d20034)
#define rPDATF		(*(volatile unsigned *)0x1d20038)
#define rPUPF		(*(volatile unsigned *)0x1d2003c)

#define rPCONG		(*(volatile unsigned *)0x1d20040)
#define rPDATG		(*(volatile unsigned *)0x1d20044)
#define rPUPG		(*(volatile unsigned *)0x1d20048)

#define rSPUCR		(*(volatile unsigned *)0x1d2004c)
#define rEXTINT		(*(volatile unsigned *)0x1d20050)
#define rEXTINTPND	(*(volatile unsigned *)0x1d20054)

/* INTERRUPT */
#define rINTCON		(*(volatile unsigned *)0x1e00000)
#define rINTPND		(*(volatile unsigned *)0x1e00004)
#define rINTMOD		(*(volatile unsigned *)0x1e00008)
#define rINTMSK		(*(volatile unsigned *)0x1e0000c)

#define rI_PSLV		(*(volatile unsigned *)0x1e00010)
#define rI_PMST		(*(volatile unsigned *)0x1e00014)
#define rI_CSLV		(*(volatile unsigned *)0x1e00018)
#define rI_CMST		(*(volatile unsigned *)0x1e0001c)
#define rI_ISPR		(*(volatile unsigned *)0x1e00020)
#define rI_ISPC		(*(volatile unsigned *)0x1e00024)

#define rF_ISPR		(*(volatile unsigned *)0x1e00038)
#define rF_ISPC		(*(volatile unsigned *)0x1e0003c)

#define	_TICK	1000				//  1/1000 sec
//#define _TICK	100				//  1/100 sec
//#define _TICK	10				//  1/10 sec
#define _CLOCK	(165*(1000/_TICK))


/* Timer */
#define rTCFG0		(*(volatile unsigned *)0x1d50000)
#define rTCFG1		(*(volatile unsigned *)0x1d50004)
#define rTCON		(*(volatile unsigned *)0x1d50008)

#define rTCNTB0		(*(volatile unsigned *)0x1d5000c)
#define rTCMPB0		(*(volatile unsigned *)0x1d50010)
#define rTCNTO0		(*(volatile unsigned *)0x1d50014)

#define rTCNTB1		(*(volatile unsigned *)0x1d50018)
#define rTCMPB1		(*(volatile unsigned *)0x1d5001c)
#define rTCNTO1		(*(volatile unsigned *)0x1d50020)

#define rTCNTB2		(*(volatile unsigned *)0x1d50024)
#define rTCMPB2		(*(volatile unsigned *)0x1d50028)
#define rTCNTO2		(*(volatile unsigned *)0x1d5002c)

#define rTCNTB3		(*(volatile unsigned *)0x1d50030)
#define rTCMPB3		(*(volatile unsigned *)0x1d50034)
#define rTCNTO3		(*(volatile unsigned *)0x1d50038)

#define rTCNTB4		(*(volatile unsigned *)0x1d5003c)
#define rTCMPB4		(*(volatile unsigned *)0x1d50040)
#define rTCNTO4		(*(volatile unsigned *)0x1d50044)

#define rTCNTB5		(*(volatile unsigned *)0x1d50048)
#define rTCNTO5		(*(volatile unsigned *)0x1d5004c)


#define BIT_TIMER0	(0x1<<13)
#define BIT_EINT4567	(0x1<<21)	
#define BIT_GLOBAL	(0x1<<26)


void uart_init(int mclk,int baud);
void uart_sendbyte(int data);
char uart_getch(void);
//void uart_printf(char *fmt,...);
void uart_sendstring(char *pt);
void delay(int time);
void print(char* s);

void port_init(void);
void board_init(void);

void ARMEnableInt(void);
void ARMDisableInt(void);
void timer_init(void);
void install_sys_timer(void);
void sys_enable_int(unsigned ISPC_BIT);
void sys_disable_int(unsigned ISPC_BIT);

#endif
